import request from '../request.js';
export default {
	getWXOpenid() { // 微信端获取openid
		return new Promise((resolve, reject) => {
			uni.getProvider({
				service: "oauth",
				success: (res) => { //获取code
					uni.login({
						provider: res.provider[0],
						success: (req) => {
							request.get({
								url: '/zzj_singleSaleApi/getOpenId',
								data: {
									js_code: req.code,
									bid: uni.getStorageSync('bid')
								}
							}).then(res => {
								if (res.code == 100 && res.data) {
									uni.setStorage({
										key: "openid",
										data: res.data.openid
									})
									uni.setStorage({
										key: "session_key",
										data: res.data.session_key
									})
									if(res.data.userInfo){
										if(res.data.userInfo.nickname != '微信用户'){
											uni.setStorage({
												key: "userinfo",
												data: res.data.userInfo
											})
										}
									}
						
									resolve(res.data.openid)
								}else{
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						}
					})
				}
			})
		})
	},
	getTTOpenid() { // 抖音端获取openid
		return new Promise((resolve, reject) => {
			uni.getProvider({
				service: "oauth",
				success: (res) => {
					uni.login({
						provider: res.provider[0],
						success: (req) => {
							uni.request({ //获取用户openID
								url: "https://developer.toutiao.com/api/apps/jscode2session",
								method: "GET",
								dataType: "json",
								data: {
									code: req.code,
									anonymous_code: req.anonymousCode,
									appid: uni.getStorageSync('appid'),
									secret: uni.getStorageSync('secret'),
									encryptedData: uni.getStorageSync('encryptedData'),
									iv: uni.getStorageSync('iv'),
								},
								success: (rsp) => {
									uni.setStorage({
										key: "openid",
										data: rsp.data.openid
									})
									uni.setStorage({
										key: "session_key",
										data: rsp.data.session_key
									})
									if(res.data.userInfo){
										if(res.data.userInfo.nickname != '微信用户'){
											uni.setStorage({
												key: "userinfo",
												data: res.data.userInfo
											})
										}
									}
									resolve(rsp.data.openid)
								}
							})
						}
					})
				}
			})
		})
	},
	
	getOpenid(){
		return new Promise((resolve, reject) => {
		// #ifdef QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-HUAWEI || QUICKAPP-WEBVIEW-UNION
			resolve('testDeviceId')
		// plus.device.getInfo({
		//     success:function(e){
		//         console.log('getDeviceInfo success: '+JSON.stringify(e));
		// 		console.log(e)
		//     },
		// 	fail:function(e){
		// 		console.log('getDeviceInfo failed: '+JSON.stringify(e));
		// 		console.log(e)
		//     }
		// })
		// #endif
		
		// #ifdef MP-WEIXIN
		uni.getProvider({
			service: "oauth",
			success: (res) => { //获取code
				uni.login({
					provider: res.provider[0],
					success: (req) => {
						request.get({
							url: '/zzj_singleSaleApi/getOpenId',
							data: {
								js_code: req.code,
								bid: uni.getStorageSync('bid')
							}
						}).then(res => {
							if (res.code == 100 && res.data) {
								uni.setStorage({
									key: "openid",
									data: res.data.openid
								})
								uni.setStorage({
									key: "session_key",
									data: res.data.session_key
								})
								if(res.data.userInfo){
									if(res.data.userInfo.nickname != '微信用户'){
										uni.setStorage({
											key: "userinfo",
											data: res.data.userInfo
										})
									}
								}
								resolve(res.data.openid)
							}else{
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					}
				})
			}
		})
		// #endif
		
		// #ifdef MP-TOUTIAO
		uni.getProvider({
			service: "oauth",
			success: (res) => {
				uni.login({
					provider: res.provider[0],
					success: (req) => {
						uni.request({ //获取用户openID
							url: "https://developer.toutiao.com/api/apps/jscode2session",
							method: "GET",
							dataType: "json",
							data: {
								code: req.code,
								anonymous_code: req.anonymousCode,
								appid: uni.getStorageSync('appid'),
								secret: uni.getStorageSync('secret'),
								encryptedData: uni.getStorageSync('encryptedData'),
								iv: uni.getStorageSync('iv'),
							},
							success: (rsp) => {
								uni.setStorage({
									key: "openid",
									data: rsp.data.openid
								})
								uni.setStorage({
									key: "session_key",
									data: rsp.data.session_key
								})
								if(res.data.userInfo){
									withCredentials: true,
									if(res.data.userInfo.nickname != '微信用户'){
										uni.setStorage({
											key: "userinfo",
											data: res.data.userInfo
										})
									}
								}
							
								resolve(rsp.data.openid)
							}
						})
					}
				})
			}
		})
		// #endif
		})
	},
	getVid() { // 获取vid
		return new Promise((resolve, reject) => {
			
			request.get({
				url: '/zzj_singleSaleApi/obtainVid', 
				data: {
					openid: uni.getStorageSync('openid'),
					bid: uni.getStorageSync('bid')
				}
			}).then(res => {
				if (res.v_id && res.v_id != 0) {
					uni.setStorage({
						key: "vid",
						data: res.v_id
					})
					resolve(res.v_id)
				}
				if (res.open_id) {
					uni.setStorage({
						key: "openid",
						data: res.open_id
					})
				}
				if (res.buyState) {
					uni.setStorage({
						key: "buyState",
						data: res.buyState
					})
				}
				if(res.open_id && res.v_id > 1){
					let info = uni.getStorageSync('userinfo');
					if(info && info.nickname != null && info.nickname != undefined && info.nickname != '微信用户'){
						request.post({
							url: '/singleSaleApi/maintainUserInformation',
							data:{
								bid:uni.getStorageSync('bid'),
								v_id:res.v_id,
								openid:res.open_id,
								nickName:info.nickname,
								headimg:info.headimg,
								sex:info.sex,
								country:info.country,
								province:info.province,
								city:info.city
							},
							success:(rep)=>{
								// console.log(rep)
							}
						})
					}
					
				}
			})
		})
	},
}
